package com.erpanderp.wam.asset.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import com.erpanderp.wam.asset.model.entity.AssetRequest;
import com.erpanderp.wam.asset.model.service.AssetRequestDAO;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class AssetHandOverBean implements Serializable{


	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	ServletContext context = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private TimeZone timeZone = TimeZone.getDefault();
	
	private String employeeName;
	private String employeeCode;
	private String selectedModule;
	private boolean renderHandoverList;
	
	 private List<AssetRequest> assetRequestList = new ArrayList<AssetRequest>();
	 private List<String> modulesList = new ArrayList<String>();
	 
	 private AssetRequestDAO assetRequestDAO =new AssetRequestDAO();
	 
	 private Map<String, String> modulesMap = new HashMap<String, String>();
	

	public AssetHandOverBean() {		
		modulesList = assetRequestDAO.getAllAssetModules();
		for (String moduleName : modulesList) {
			modulesMap.put(moduleName, moduleName);
		}
	}
	
	//SEARCH ACTION
	public String searchHandoverAssets(){
			System.out.println("Search Action...");
				
			assetRequestList = assetRequestDAO.getAllHandoverAssets(selectedModule,employeeCode,employeeName);
			
			if(assetRequestList != null && assetRequestList.size() > 0){
				renderHandoverList=true;
			}else{
				renderHandoverList=false;
			}		
			return null;
	}
	
	public String assetHandoverAction(ActionEvent ae){
		System.out.println("Search Action...assetHandoverAction");
		 try{
			  Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
			  
			  String assetCode = params.get("assetCode");			  
			  FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("assetCode", assetCode);
			  		  
			  FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/asset/assethandoverdetails.html?assetCode="+assetCode);
			
			  }catch (Exception e) {
				  System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
				  e.printStackTrace();
			 }
			 return "assethandoverdetails";	
    }

	//SETTER AND GETTERS
	public String getEmployeeName() {
		return employeeName;
	}

	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}

	public String getEmployeeCode() {
		return employeeCode;
	}

	public void setEmployeeCode(String employeeCode) {
		this.employeeCode = employeeCode;
	}

	public String getSelectedModule() {
		return selectedModule;
	}

	public void setSelectedModule(String selectedModule) {
		this.selectedModule = selectedModule;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public List<AssetRequest> getAssetRequestList() {
		return assetRequestList;
	}

	public void setAssetRequestList(List<AssetRequest> assetRequestList) {
		this.assetRequestList = assetRequestList;
	}

	public AssetRequestDAO getAssetRequestDAO() {
		return assetRequestDAO;
	}

	public void setAssetRequestDAO(AssetRequestDAO assetRequestDAO) {
		this.assetRequestDAO = assetRequestDAO;
	}

	public boolean isRenderHandoverList() {
		return renderHandoverList;
	}

	public void setRenderHandoverList(boolean renderHandoverList) {
		this.renderHandoverList = renderHandoverList;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public TimeZone getTimeZone() {
		return timeZone;
	}

	public void setTimeZone(TimeZone timeZone) {
		this.timeZone = timeZone;
	}

}
